package com.kayden.springboot.controller;


import com.kayden.springboot.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

//@RestController
@RequestMapping("/request")
@ResponseBody
@Controller
public class RequestController {

    @RequestMapping("/r1")
    public String r1(String keyword) {
        return "接收参数："+keyword;
    }

    @RequestMapping("/r2")
    public String r2(String userName, String password) {
        return "用户名: "+ userName + "密码: "+password;
    }

    @RequestMapping("/r3")
    public String r3(Integer number) {//Integer是分装类，会自定义初始值为null，不传参就默认为null
        return "number: "+ number;
    }

    @RequestMapping("/r4")
    public String r4(int number) {//int 就得传参
        return "number: "+ number;
    }

    @RequestMapping("/r5")//UserInfo自定义类，会有默认初始值，不传参默认为null，0，0
    public String r5(UserInfo userInfo) {
        return "userInfo: "+ userInfo.toString();
    }

    //从前端接收参数q，赋值给keyword
    @RequestMapping("/r6")//RequestParam后端重命名
    public String r6(@RequestParam("q") String keyword){//如果不加require，则默认为必须项，就不能用keyword来传参，只能用q
        return "keyword: "+ keyword;
    }

    @RequestMapping("/r66")
    public String r66(@RequestParam(value = "q", required = false) String keyword){
        return "keyword: "+ keyword;
    }

    //传递数组
    @RequestMapping("/r7")
    public String r7(String[] arr){
        return "arr: "+ Arrays.toString(arr);
    }

    //传递集合
    @RequestMapping("/r8")
    public String r8(@RequestParam List<Integer> list){
        return "list: "+ list;
    }

    //传递Json
    @RequestMapping("/r9")
    public String r9(@RequestBody UserInfo userInfo){
        System.out.println(userInfo.getName());
        return userInfo.toString();
    }

    //从url中获取参数
    @RequestMapping("/article/{articleId}")
    public String r10(@PathVariable Integer articleId){
        return "articleId: "+ articleId;
    }

    @RequestMapping("/article/{type}/{articleId}")
    public String r11(@PathVariable Integer articleId,
                      @PathVariable("type") Integer articleType){
        return "articleId: "+ articleId + "type: "+ articleType;
    }

    //上传文件
    @RequestMapping("/r12")
    public String r12(@RequestPart("file") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        //文件上传
        file.transferTo(new File("/Users/cairunkai/Desktop/文件上传/"+ file.getOriginalFilename()));
        return "success";
    }


    @RequestMapping("/r13")
    public String r13(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        StringBuilder builder = new StringBuilder();
        if(cookies != null){
            for(Cookie cookie : cookies){
                System.out.println(cookie.getName());
                builder.append(cookie.getName()+":"+cookie.getValue());
            }
        }
        return "返回Cookie成功 ,cookies:" +builder.toString();

    }

    @RequestMapping("/r14")
    public String r14(@CookieValue("java") String java){
        return "从Cookie中获取Java的值："+java;
    }

    //存储session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        //从cookie中获取sessionId，根据sessionId获取Session对象
        //如果sessionId不存在，则创建
        HttpSession session = request.getSession();
        //默认存储在内存中
        //登陆的用户名称
        session.setAttribute("username","zhangsan");
        session.setAttribute("age","18");
        return "设置session成功";
    }

    //获取session
    @RequestMapping("/getsession")
    public String getSession(HttpServletRequest request) {
        //从cookie中获取sessionId，根据sessionId获取Session对象
        HttpSession session = request.getSession(false);
        //如果用户登陆，session有值，未登陆，session为null
        if (session != null) {
            return "用户未登陆";
        }else{
            //从session中获取用户信息
            String userName = (String) session.getAttribute("username");
            return "登陆用户为："+userName;
        }
    }

    //获取session
    @RequestMapping("/getsession2")
    public String getSession2(HttpSession session){
        //从session中获取登陆用户的信息
        String username = (String) session.getAttribute("username");
        return "登陆用户为："+username;
    }

    //获取session
    @RequestMapping("/getsession3")
    public String getSession3(@SessionAttribute("userName") String username){
        return "登陆用户为： "+username;
    }


    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return "从header中获取userAgent:"+userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return "从header中获取userAgent:"+userAgent;
    }
}

